package com.passionfruit.module.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.passionfruit.common.utils.Constant;
import com.passionfruit.module.entity.SysMenu;
import com.passionfruit.module.entity.SysRoleMenu;
import com.passionfruit.module.entity.SysUserRole;
import com.passionfruit.module.mapper.SysMenuMapper;
import com.passionfruit.module.service.ISysMenuService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.passionfruit.module.service.ISysRoleMenuService;
import com.passionfruit.module.service.ISysUserService;
import com.xiaoleilu.hutool.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单管理 服务实现类
 * </p>
 *
 * @author weirdor
 * @since 2017-10-31
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private ISysRoleMenuService sysRoleMenuService;

    @Override
    public List<SysMenu> queryListParentId(Long parentId, List<Long> menuIdList) {
        List<SysMenu> menuList = queryListParentId(parentId);
        if(menuIdList == null){
            return menuList;
        }

        List<SysMenu> userMenuList = new ArrayList<>();
        menuList.forEach(k ->{
            if(menuIdList.contains(k.getMenuId())){
                userMenuList.add(k);
            }
        });
        return userMenuList;
    }

    @Override
    public List<SysMenu> queryListParentId(Long parentId) {
        EntityWrapper<SysMenu> ew=new EntityWrapper<>();
        ew.where("parent_id={0}",parentId).orderBy("order_num",true);
        return  super.selectList(ew);
    }

    @Override
    public List<SysMenu> queryNotButtonList() {
        EntityWrapper<SysMenu> ew=new EntityWrapper<>();
        ew.where("type!=2").orderBy("order_num",true);
        return super.selectList(ew);
    }

    @Override
    public List<SysMenu> getUserMenuList(Long userId) {
        //系统管理员，拥有最高权限
        if(userId == Constant.SUPER_ADMIN){
            return getAllMenuList(null);
        }

        //用户菜单列表
        List<Long> menuIdList = sysUserService.queryAllMenuId(userId);
        return getAllMenuList(menuIdList);
    }

    @Override
    public SysMenu queryObject(Long menuId) {
        return super.selectById(menuId);
    }

    @Override
    public List<SysMenu> queryList(Map<String, Object> map) {
        return sysMenuMapper.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        EntityWrapper<SysMenu> ew=new EntityWrapper<>();
        return super.selectCount(ew);
    }

    @Override
    public void save(SysMenu menu) {

        super.insert(menu);
    }

    @Override
    public void update(SysMenu menu) {
        super.updateAllColumnById(menu);

    }

    @Override
    public void deleteBatch(Long[] menuIds) {
      super.deleteBatchIds(CollectionUtil.newArrayList(menuIds));
        EntityWrapper<SysRoleMenu> ew = new EntityWrapper<>();
        ew.where("menu_id").in("menu_id", menuIds);
        sysRoleMenuService.delete(ew);

    }

    @Override
    public List<SysMenu> queryUserList(Long userId) {
        return sysMenuMapper.queryUserList(userId);
    }

    /**
     * 获取所有菜单列表
     */
    private List<SysMenu> getAllMenuList(List<Long> menuIdList){
        //查询根菜单列表
        List<SysMenu> menuList = queryListParentId(0L, menuIdList);
        //递归获取子菜单
        getMenuTreeList(menuList, menuIdList);

        return menuList;
    }

    /**
     * 递归
     */
    private List<SysMenu> getMenuTreeList(List<SysMenu> menuList, List<Long> menuIdList){
        List<SysMenu> subMenuList = new ArrayList<SysMenu>();
        menuList.forEach(k->{
            if(k.getType() == Constant.MenuType.CATALOG.getValue()){//目录
                k.setList(getMenuTreeList(queryListParentId(k.getMenuId(), menuIdList), menuIdList));
            }
            subMenuList.add(k);
        });
        return subMenuList;
    }
}
